package com.service.impl;

import com.constant.Constant;
import com.entity.Resource;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.mapper.ResourceMapper;
import com.service.ResourceService;
import com.vo.ResourceAndPageInfoVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * Created with IntelliJ IDEA.
 *
 * @Author: 陈忠杰
 * @Date: 2021/07/01/13:45
 * @Description:
 */
@Service
public class ResourceServiceImpl implements ResourceService {

    @Autowired
    ResourceMapper resourceMapper;


    /**
    * @Description: 返回的是实体类Resource对象的PageInfo（仅仅包含User对象的login_name属性加上其他基本数据类型属性）
    * @Param: [userId , pageNo ]
    * @return: com.github.pagehelper.PageInfo<com.entity.Resource>
    * @Author: 陈忠杰
    * @Date: 2021/7/1
    */
    @Override
    public PageInfo<Resource> getResourceByUserIdAndPageNo(int userId, int pageNo) {
        PageHelper.startPage(pageNo, Constant.PAGE_SIZE_COURSE);
        List<Resource> resources = resourceMapper.selResourceByUserIdAndPageNo(userId);
        PageInfo<Resource> resourcePageInfo = new PageInfo<>(resources);
        return resourcePageInfo;
    }

    @Override
    public int addResource(String title, String path, String original_name, long file_size, String file_type, int cost_type, int cost_number, int userId, int chapter_id, int status) {
        Date date = new Date();
        int i = resourceMapper.insResource(title, path, original_name, file_size, file_type, date,cost_type, cost_number, userId, chapter_id, status);
        return i;
    }

    @Override
    public int updResource(String title, String path, String original_name, long file_size, String file_type, int cost_type, int cost_number, int userId, int chapter_id, int status, int id) {
        Date date = new Date();
        int i = resourceMapper.updResource(title, path, original_name, file_size, file_type, date,cost_type, cost_number, userId, chapter_id, status,id);
        return i;
    }

    @Override
    public int delResource(int id) {
        int i = resourceMapper.delResource(id);
        return i;
    }

    @Override
    public int changeStatus(int id, int status) {
        int i = resourceMapper.updStatus(id, status);
        return i;
    }

    @Override
    public PageInfo<Resource> findBackResourceByInfo(ResourceAndPageInfoVO resourceAndPageInfoVO) {
        PageHelper.startPage(resourceAndPageInfoVO.getPageNo(),Constant.PAGE_SIZE_COURSE);
        List<Resource> resources = resourceMapper.selResourceByInfo(resourceAndPageInfoVO);
        PageInfo<Resource> resourcePageInfo = new PageInfo<>(resources);
        return resourcePageInfo;
    }

    @Override
    public Resource getResourceById(int id) {
        Resource resource = resourceMapper.selResourceById(id);
        return resource;
    }
}
